package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.QrCodeInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.QrCodeInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.QrCodeInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.QrCodeInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntQrCodeQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntQrCodeQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户收款聚合码查询服务
 * Created by 王成 on 2018/10/18.
 */
public class MchntQrCodeQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntQrCodeQueryRequest mchntQrCodeQueryRequest = new MchntQrCodeQueryRequest();
        ConvertUtil.convertOutput(mchntQrCodeQueryRequest);
        /* 参数合法性校验 */
        if (null == mchntQrCodeQueryRequest.getApplyBatchNo() && null == mchntQrCodeQueryRequest.getQrCode() && null == mchntQrCodeQueryRequest.getQrUrl()) {
            rglog.error("商户收款聚合码查询服务处理失败，参数异常，申请批次号<{}> ，商户收款聚合码<{}>,商户收款URL<{}> !",
                    mchntQrCodeQueryRequest.getApplyBatchNo(), mchntQrCodeQueryRequest.getQrCode(), mchntQrCodeQueryRequest.getQrUrl());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (mchntQrCodeQueryRequest.getPageIndex() < -1 || mchntQrCodeQueryRequest.getPageSize() < -1) {
            rglog.error("商户收款聚合码查询服务处理失败，页码:{}，页数:{},校验不通过。",
                    mchntQrCodeQueryRequest.getPageIndex(), mchntQrCodeQueryRequest.getPageSize());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        return mchntQrCodeQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntQrCodeQueryRequest mchntQrCodeQueryRequest = (MchntQrCodeQueryRequest) request;
        MchntQrCodeQueryResponse mchntQrCodeQueryResponse = new MchntQrCodeQueryResponse();
        String instId = mchntQrCodeQueryRequest.getHeader().getInstId();

        /* 根据申请批次号查询商户收款聚合码信息 */
        QrCodeInfoMapper qrCodeInfoMapper = new QrCodeInfoMapperImpl();
        if (!StringUtil.isEmptyOrNull(mchntQrCodeQueryRequest.getApplyBatchNo())) {
            int pageIndex = mchntQrCodeQueryRequest.getPageIndex();
            int pageSize = mchntQrCodeQueryRequest.getPageSize();
            if (-1 == pageIndex || -1 == pageSize) {
                List<QrCodeInfo> qrCodeInfoList = qrCodeInfoMapper.selectQrCodeInfoByApplyBatchNo(instId, mchntQrCodeQueryRequest.getApplyBatchNo());
                if (CollectionUtils.isEmpty(qrCodeInfoList)) {
                    mchntQrCodeQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    mchntQrCodeQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                } else {
                    List<QrCodeInfoBean> mchntQrCodeInfoList = qrCodeInfoList.stream().map(this::convertMchntQrCodeInfo).collect(Collectors.toList());
                    //过滤前总记录数
                    int totalRows = mchntQrCodeInfoList.size();
                    //总记录数
                    mchntQrCodeQueryResponse.setTotalRows(totalRows);
                    mchntQrCodeQueryResponse.setQrCodeInfoList(mchntQrCodeInfoList);
                }
            } else {
                PageInfo pageInfo = new PageInfo();
                pageInfo.setPageIndex(pageIndex);
                pageInfo.setPageSize(pageSize);
                PageResult pageResult = qrCodeInfoMapper.selectQrCodeInfoByApplyBatchNo(instId, mchntQrCodeQueryRequest.getApplyBatchNo(), pageInfo);
                if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                    rglog.error("根据查询条件查询二维码信息 pageResult 为null !");
                    throw new BizException(RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespDesc());
                }
                /* 根据取出记录条数做对应处理 */
                if (pageResult.getResult().isEmpty()) {
                    rglog.info("根据查询条件查询到0条二维码信息!");
                    mchntQrCodeQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    mchntQrCodeQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                } else {
                    List<QrCodeInfo> qrCodeInfoList = new ArrayList<>();
                    for (Object object : pageResult.getResult()) {
                        qrCodeInfoList.add((QrCodeInfo) object);
                    }
                    rglog.info("SIZE=<{}>", pageResult.getResult().size());
                    List<QrCodeInfoBean> mchntQrCodeInfoList = qrCodeInfoList.stream().map(this::convertMchntQrCodeInfo).collect(Collectors.toList());
                    mchntQrCodeQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
                    mchntQrCodeQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                    mchntQrCodeQueryResponse.setQrCodeInfoList(mchntQrCodeInfoList);
                }
            }
        } else {
            if (!StringUtil.isEmptyOrNull(mchntQrCodeQueryRequest.getQrUrl())) {
                QrCodeInfo qrCodeInfo = qrCodeInfoMapper.selectQrCodeInfoByPrimaryKey(instId, mchntQrCodeQueryRequest.getQrUrl());
                if (null != qrCodeInfo) {
                    List<QrCodeInfoBean> mchntQrCodeInfoList = Collections.singletonList(convertMchntQrCodeInfo(qrCodeInfo));
                    mchntQrCodeQueryResponse.setQrCodeInfoList(mchntQrCodeInfoList);
                    mchntQrCodeQueryResponse.setTotalRows(1);
                    mchntQrCodeQueryResponse.setTotalPage(1);
                } else {
                    mchntQrCodeQueryResponse.setTotalRows(0);
                }
            } else if (!StringUtil.isEmptyOrNull(mchntQrCodeQueryRequest.getQrCode())) {
                QrCodeInfo qrCodeInfo = qrCodeInfoMapper.selectQrCodeInfoByQrCode(instId, mchntQrCodeQueryRequest.getQrCode());
                if (null != qrCodeInfo) {
                    List<QrCodeInfoBean> mchntQrCodeInfoList = Collections.singletonList(convertMchntQrCodeInfo(qrCodeInfo));
                    mchntQrCodeQueryResponse.setQrCodeInfoList(mchntQrCodeInfoList);
                    mchntQrCodeQueryResponse.setTotalRows(1);
                    mchntQrCodeQueryResponse.setTotalPage(1);
                } else {
                    mchntQrCodeQueryResponse.setTotalRows(0);
                }
            }
        }

        /* 组装应答信息 */
        BizResponse<MchntQrCodeQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntQrCodeQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntQrCodeQueryResponse mchntQrCodeQueryResponse = (MchntQrCodeQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntQrCodeQueryResponse);
    }

    /**
     * 商户收款聚合码转换
     *
     * @param qrCodeInfo 商户收款码信息
     */
    private QrCodeInfoBean convertMchntQrCodeInfo(QrCodeInfo qrCodeInfo) {
        QrCodeInfoBean qrCodeInfoBean = new QrCodeInfoBean();
        BeanUtil.beanCopy(qrCodeInfo, qrCodeInfoBean);
        return qrCodeInfoBean;
    }
}
